$(function(){
	$(".setting_form")
		.y_setting()
		.auto_check({
			'submit_btn' 	: '.save_btn',
			'next_focus'	: true
		})
		.ajax_form({
			'submit_btn' 	: '.save_btn',
			'form_position'	: null,
			'success' 		: function(form, data){
				window.location.href = '/manage/ad/edit/' + data;
			}
		});

	//注册编辑器窗口自动适应窗口大小
	addWinResize(function(){
		$('.editor .edui-editor').width($('.setting_content').width() - 20);
	});

	$(document).on('click', '.del_link', function(){
		if(!confirm('是否继续删除？')){
			return;
		}
		$(this).parents('.item').slideUp(function(){
			$(this).remove();
			if(!$('#ad_ul>li').length){
				//无记录
				$('#ad_ul').append('<li class="no_record"><img src="/images/80x80/no_picture.jpg"/>暂无记录！</li>');
			}
		});

		
	});

	$('#ad_ul .img>img').auto_img();

	$('.date').datepicker({
		dateFormat : 'yy-mm-dd',
		monthNames : ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
		dayNamesMin : [ '日','一', '二', '三', '四', '五', '六']
	});

	$('#add_text_ad').click(function(){
		y_on_uploaded_ad_image_list($('#ad_ul'), null, 'pictures[]', '100x100')
	});

	$('#ad_ul .uploader').uploader({
		/**
		 * echo json_encode(array(
			'url' 		=> $info['url'],
			'name' 		=> $info['name'],
			'title' 	=> $info['title'],
			'original' 	=> $info['originalName'],
			'state' 	=> $info['state']
		));
		 */
		on_complete : function(img_id, data){
			var image_path = '/uploads/temp/100x100/' + data.name;
			var img_content = $('#' + img_id).parents('.img_content');
			$('.img>img', img_content).attr('src', image_path).auto_img();
			$('.img>input', img_content).val('/' + data.url);
		}
	});
});

/**
 * 图片上传完成后执行
 */
function y_on_uploaded_ad_image_list(ul_id, file, name, thumb){
	$('#ad_ul>.no_record').remove();
	var image_path = null;
	var image_path_val = '';
	if(file === null){
		image_path = '/images/' + thumb + '/no_picture.jpg'
	}else{
		image_path = '/uploads/temp/' + thumb + '/' + file.name;
		image_path_val = '/uploads/temp/' + file.name;
	}
	var li = $(
		'<li class="item">' + 
		'	<div class="img_content">' + 
		'		<div class="img">' + 
		'			<img src="' + image_path + '"/>' + 
		'			<input type="hidden" name="' + name + '" value="' + image_path_val + '"/>' +
		'		</div>' + 
		'		（点击修改图片）' +
		'	</div>' +
		'	<div class="item_info">' +
		'		<div class="row">' + 
		'			<strong>广告标题：</strong> ' + 
		'			<input type="text" class="ad_item_title" name="titles[]" rule="^.+$"/>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>广告链接：</strong> ' + 
		'			<input type="text" name="urls[]" />' +
		'			<p>点击广告后打开的网址</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>打开位置：</strong> ' + 
		'			<select name="targets[]">' + 
		'				<option value="_blank">新窗口</option>' + 
		'				<option value="_top">当前窗口</option>' + 
		'			</select>' +
		'			<p>打开网址的位置</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>有效期：</strong> ' + 
		'			<input type="text" name="starts[]" class="date" rule="^$|^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"/> - ' + 
		'			<input type="text" name="ends[]" class="date" rule="^$|^\\d{4}\\-\\d{1,2}\\-\\d{1,2}$"/>' +
		'			<p>为空表示不作限制，超过时间后，将自动隐藏</p>' +
		'		</div>' +
		'		<div class="row">' + 
		'			<strong>备注：</strong> ' + 
		'			<textarea name="descs[]"></textarea>' +
		'		</div>' +
		'	</div>' + 
		'	<div class="options">' +
		'		<a href="javascript:void(0)" class="del_link">删除</a>' +
		'	</div>' +
		'</li>'
	);
	$('#ad_ul').append(li);
	li.auto_check({
		'submit_btn' : '.save_btn'
	});
	li.find('img').auto_img();
}